package com.sdtv.mediunfusion.mvp.ui.outsidetask;


import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sdtv.basemodule.annotations.CreatePresenter;
import com.sdtv.basemodule.annotations.PresenterVariable;
import com.sdtv.basemodule.base.BaseActivity;
import com.sdtv.basemodule.router.ARouterUtils;
import com.sdtv.basemodule.utils.ClickProxy;
import com.sdtv.mediunfusion.R;
import com.sdtv.mediunfusion.bean.TaskBean;
import com.sdtv.mediunfusion.mvp.contract.MainContract;
import com.sdtv.mediunfusion.mvp.presenter.TaskListPresenter;
import com.sdtv.mediunfusion.mvp.ui.outsidetask.adapter.TaskListAdapter;
import com.sdtv.mediunfusion.utils.Constants;
import com.sdtv.mediunfusion.utils.RouterPath;
import com.sdtv.mediunfusion.widget.CustomeToolBar;
import com.sdtv.mediunfusion.widget.FilterView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 外采任务列表页
 * --
 */
@CreatePresenter(presenter = TaskListPresenter.class)
@Route(path = RouterPath.TO_TASK_LIST_Activity)
public class ExternalTaskListActivity extends BaseActivity implements
        MainContract.TaskListView, FilterView.ChooseTaskListener {

    @BindView(R.id.taskListView)
    RecyclerView listView;
    @BindView(R.id.taskList_xRefreshView)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.taskList_statusView)
    LinearLayout statusLayout;
    @BindView(R.id.taskList_parentView)
    RelativeLayout parentView;
    @BindView(R.id.et_titleBar)
    CustomeToolBar etTitleBar;
    private TaskListAdapter adapter;
    private FilterView filterView;
    // 任务列表的工具类
    @PresenterVariable
    TaskListPresenter presenter;
    // 列表数据
    private List<TaskBean> taskListData = new ArrayList<>();
    // 当前加载的页数
    private int currentPage = 0;
    // 当前选中的任务列表状态
    private String selectStatus = "0";
    // 搜索关键词
    private String keyword = "";

    /**
     * 设置布局文件
     *
     * @return
     */
    @Override
    public int getLayoutId() {
        return R.layout.activity_external_task_list;
    }

    /**
     * 初始化view
     */
    @Override
    public void initView() {
        // 设置列表的adapter
        adapter = new TaskListAdapter(R.layout.task_item_layout, taskListData);
        listView.setLayoutManager(new LinearLayoutManager(this));
        listView.setAdapter(adapter);
        //设置刷新事件
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            // 刷新列表数据
            taskListData = new ArrayList<>();
            currentPage = 0;
            loadData();
        });
        // 设置加载更多事件
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            currentPage += Constants.PAGE_SIZE;
            loadData();
        });
        //设置listview的点击事件
        adapter.setOnItemClickListener((adapter, view, position) -> {
            TaskBean taskBean = (TaskBean) adapter.getItem(position);
            HashMap<String, String> map = new HashMap<>();
            map.put("taskId", taskBean.getDistributionTaskKey());
            //如果当前是待领取状态  需要跳转到待领取页面
            if ("4".equals(taskBean.getUserTaskStatus())) {
//                    RouteSkip.pathCompose(ExternalTaskListActivity.this, RouterConfig.UNCLAIMED_PAGE, map, true);
            } else {
//                    RouteSkip.pathCompose(ExternalTaskListActivity.this, RouterConfig.TASKDETAILPAGE, map, true);
            }
        });
        //添加加载statusView
        filterView = new FilterView(this, this);
        statusLayout.addView(filterView);
        //我的选题点击事件
        etTitleBar.getTbLogText().setOnClickListener(new ClickProxy(view -> {
            //点击跳转到我的选题页面
            ARouterUtils.navigation(RouterPath.TO_SELECT_TOPIC_LIST_ACTIVITY);
        }));
    }

    /**
     * 加载列表数据
     */
    @Override
    public void initData() {
    }

    private void loadData() {
        presenter.getTaskList(currentPage, selectStatus, keyword);
    }

    /**
     * 每次进入页面都需要刷新数据
     */
    @Override
    protected void onResume() {
        super.onResume();
        // 由于每次进入该页面都需要刷新数据 所以在这里刷新了一下
        currentPage = 0;
        taskListData = new ArrayList<>();
        keyword = "";
        loadData();

    }

    /**
     * 加载到列表数据
     *
     * @param totalCount
     * @param responseList
     */
    @Override
    public void setListData(int totalCount, List<TaskBean> responseList) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        taskListData.addAll(responseList);
        adapter.setNewData(taskListData);
        // 判断是否显示加载更多
        if (totalCount <= taskListData.size()) {
            //全部数据已经加载完成 不显示加载更多
            refreshLayout.finishLoadMoreWithNoMoreData();
        }
    }

    /**
     * 加载数据为空
     * --显示EMPTYVIEW 或者是 未加载到数据
     */
    @Override
    public void showEmptyView() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        if (null != adapter && adapter.getData().isEmpty()) {
            // 显示暂无内容
            adapter.setEmptyView(R.layout.tasklist_nocontent_layout, listView);
        } else {
            refreshLayout.finishLoadMoreWithNoMoreData();
        }
    }

    /**
     * 加载页面失败--
     */
    @Override
    public void onError() {
        super.onError();
    }

    @Override
    public void getSelectStatusAndKey(String status, String key) {
        this.selectStatus = status;
        this.keyword = key;
        currentPage = 0;
        taskListData = new ArrayList<>();
        loadData();
    }
}
